$(function () {
    $("#jqGrid").Grid({
        url: '../target/list',
        colModel: [
            {label: '月份', name: 'month', index: 'month', width: 80},
            {label: '业务员', name: 'agentInfo.username', width: 80},
            {
                label: '计划签单数',
                name: 'planTotalOrder',
                index: 'plan_total_order',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    return cellValue + "个";
                }
            },
            {
                label: '实际签单数',
                name: 'totalOrder',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    let info = cell['orderStatisticsInfo'];
                    if (null == info) {
                        return "0个";
                    }
                    return info['totalOrder'] + "个";
                }
            },
            {
                label: '完成进度',
                name: 'processOfOrder',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    return cellValue + "%";
                }
            },
            {
                label: '计划订单金额',
                name: 'planTotalOrderAmount',
                index: 'plan_total_order_amount',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    return cellValue + "万";
                }
            },
            {
                label: '实际签单金额',
                name: 'totalOrderAmount',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    let info = cell['orderStatisticsInfo'];
                    if (null == info) {
                        return "0万";
                    }
                    return info['totalOrderAmount'] + "万";
                }
            },
            {
                label: '完成进度',
                name: 'processOfMoney',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    return cellValue + "%";
                }
            },
            {label: '最近修改时间', name: 'updateAt', index: 'update_at', width: 80},
            {
                label: '操作',
                name: 'id',
                index: 'id',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    return "<a class='detail-link-btn' data-id='" + cellValue + "'>查看</a>"
                }
            }]
    });

    $(document)
        .off("click", ".detail-link-btn")
        .on("click", ".detail-link-btn", function () {
            let id = $(this).data("id");
            details(id);
        })
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        detailAction: false,
        title: null,
        target: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空'}
            ]
        },
        q: {
            name: '',
            month: ''
        },
        options: {}
    },
    mounted: function () {
        let vmObj = this;
        Ajax.request({
            url: "../common/getOptionValues",
            type: "GET",
            contentType: "application/json",
            async: true,
            successCallback: function (r) {
                vmObj.options = r;
            }
        });

        // methods里面定义的方法, 需要赋值给window
        window.details = this.details;
    },
    methods: {
        details: function (id) {
            vm.showList = false;
            vm.detailAction = true;
            vm.getInfo(id);
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.detailAction = false;
            vm.title = "新增";
            vm.target = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.detailAction = false;
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.target.id == null ? "../target/save" : "../target/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.target),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../target/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../target/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.target = r.target;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, "month": vm.q.month},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                month: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});